/**
 * 
 */
package com.ihg.hiex.entity.session;

import java.beans.Transient;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.format.PersistentFormat;
import com.ihg.hiex.entity.data.reference.Phase;
import com.ihg.hiex.entity.user.Administrator;

/**
 * 
 * @author downpour
 * @author mohanlan
 */
@Entity
@Table(name = "session")
public class Session {

    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    private String description;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;
    
    /** 共有几组 */
    private int rooms;
    
    /** assessor 数量 */
    private int assessorCount;
    
    /** delegate 数量 */
    private int delegateCount;
    
    /** session 状态 */
    @Enumerated(EnumType.STRING)
    private Status sessionStatus;
    
    /** session active状态中的 */
    @Enumerated(EnumType.STRING)
    private Phase sessionPhase;
    
    /** Session Host */
    @PersistentFormat
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Administrator sessionHost;
    
    /** Assessor Owner*/
    @PersistentFormat
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Administrator assessorOwner;
    
    /** Delegate Owner*/
    @PersistentFormat
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Administrator delegateOwner;
    
    /** Assessor Wechat Group */
    private String assessorWechatGroup;
    
    /** Delegate WechatGroup */
    private String delegateWechatGroup;
    
    /** 创建时间 */
    private Date createTime;
    
    private Date lastUpdateTime;
    
    private Long lastOperator;
    
    @Column(name = "could_send_email")
    private boolean couldSendEmail;
    
    @Column(name = "is_disabled")
    private boolean disabled;

    /**
     * The default constructor.
     */
    public Session() {
    }
    
    /**
     * 
     * @param sessionHost
     * @param assessorCount
     * @param delegateCount
     * @param assessorWechatGroup
     * @param delegateWechatGroup
     * @param operator
     * @return
     */
    public Session initialize(Administrator sessionHost, int assessorCount, int delegateCount, String assessorWechatGroup, String delegateWechatGroup, Long operator) {
        this.sessionHost = sessionHost;
        this.assessorCount = assessorCount;
        this.delegateCount = delegateCount;
        this.assessorWechatGroup = assessorWechatGroup;
        this.delegateWechatGroup = delegateWechatGroup;
        this.sessionStatus = Status.NOTSTARTED;
        this.createTime = new Date();
        this.lastUpdateTime = createTime;
        this.lastOperator = operator;
        this.disabled = false;
        this.couldSendEmail = false;
        return this;
    }
    
    /**
     * 
     * @param assessorCount
     * @param delegateCount
     * @param operator
     * @return
     */
    public Session edit(int assessorCount, int delegateCount, Long operator) {
        this.assessorCount = assessorCount;
        this.delegateCount = delegateCount;
        this.lastUpdateTime = new Date();
        this.lastOperator = operator;
        return this;
    }
    
    /**
     * @param sessionHost
     * @return
     */
    public Session editSessionHost(Administrator sessionHost) {
        this.sessionHost = sessionHost;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public Session openNotice() {
      this.couldSendEmail = true;
      return this;
    }
    
    /**
     * 
     * @param operator
     * @return
     */
    public Session start(Long operator) {
        this.sessionStatus = Status.ACTIVE;
        this.sessionPhase = Phase.BEFORE;
        this.lastUpdateTime = new Date();
        this.lastOperator = operator;
        return this;
    }
    
    /**
     * 
     * @param operator
     * @return
     */
    public Session delete(Long operator) {
        this.disabled = true;
        this.lastUpdateTime = new Date();
        this.lastOperator = operator;
        return this;
    }
    
    /**
     * 
     * @param operator
     * @return
     */
    public Session close(Long operator) {
        this.sessionStatus = Status.CLOSE;
        this.lastUpdateTime = new Date();
        this.lastOperator = operator;
        return this;
    }
    
    /**
     * 
     * @param phase
     * @return
     */
    public Session changePhase(Phase phase) {
        this.sessionPhase = phase;
        return this;
    }
    
    /**
     * @return
     */
    @Transient
    public Date getSecnodDate() {
        return DateUtils.addDays(this.startDate, 1);
    }

    /**
     * @return
     */
    @Transient
    public Date getCoachStartDate() {
        return DateUtils.addDays(this.endDate, 1);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public Date getCoachEndDate() {
        return DateUtils.addYears(this.getCoachStartDate(), 1);
    }

    public Long getId() {
      return id;
    }

    public void setId(Long id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getDescription() {
      return description;
    }

    public void setDescription(String description) {
      this.description = description;
    }

    public Date getStartDate() {
      return startDate;
    }

    public void setStartDate(Date startDate) {
      this.startDate = startDate;
    }

    public Date getEndDate() {
      return endDate;
    }

    public void setEndDate(Date endDate) {
      this.endDate = endDate;
    }

    public int getRooms() {
      return rooms;
    }

    public void setRooms(int rooms) {
      this.rooms = rooms;
    }

    public int getAssessorCount() {
      return assessorCount;
    }

    public void setAssessorCount(int assessorCount) {
      this.assessorCount = assessorCount;
    }

    public int getDelegateCount() {
      return delegateCount;
    }

    public void setDelegateCount(int delegateCount) {
      this.delegateCount = delegateCount;
    }

    public Status getSessionStatus() {
      return sessionStatus;
    }

    public void setSessionStatus(Status sessionStatus) {
      this.sessionStatus = sessionStatus;
    }

    public Phase getSessionPhase() {
      return sessionPhase;
    }

    public void setSessionPhase(Phase sessionPhase) {
      this.sessionPhase = sessionPhase;
    }

    public Administrator getSessionHost() {
      return sessionHost;
    }

    public void setSessionHost(Administrator sessionHost) {
      this.sessionHost = sessionHost;
    }

    public Administrator getAssessorOwner() {
      return assessorOwner;
    }

    public void setAssessorOwner(Administrator assessorOwner) {
      this.assessorOwner = assessorOwner;
    }

    public Administrator getDelegateOwner() {
      return delegateOwner;
    }

    public void setDelegateOwner(Administrator delegateOwner) {
      this.delegateOwner = delegateOwner;
    }

    public String getAssessorWechatGroup() {
      return assessorWechatGroup;
    }

    public void setAssessorWechatGroup(String assessorWechatGroup) {
      this.assessorWechatGroup = assessorWechatGroup;
    }

    public String getDelegateWechatGroup() {
      return delegateWechatGroup;
    }

    public void setDelegateWechatGroup(String delegateWechatGroup) {
      this.delegateWechatGroup = delegateWechatGroup;
    }

    public Date getCreateTime() {
      return createTime;
    }

    public void setCreateTime(Date createTime) {
      this.createTime = createTime;
    }

    public Date getLastUpdateTime() {
      return lastUpdateTime;
    }

    public void setLastUpdateTime(Date lastUpdateTime) {
      this.lastUpdateTime = lastUpdateTime;
    }

    public Long getLastOperator() {
      return lastOperator;
    }

    public void setLastOperator(Long lastOperator) {
      this.lastOperator = lastOperator;
    }

    public boolean isCouldSendEmail() {
      return couldSendEmail;
    }

    public void setCouldSendEmail(boolean couldSendEmail) {
      this.couldSendEmail = couldSendEmail;
    }

    public boolean isDisabled() {
      return disabled;
    }

    public void setDisabled(boolean disabled) {
      this.disabled = disabled;
    }
}
